package com.fhz.a2102shopjava.ui.home;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.entity.GoodsInfo;
import com.fhz.a2102shopjava.ui.details.DetailsActivity;
import com.fhz.a2102shopjava.util.ImageLoader;

import java.util.List;

/**
 * 时间:2023/8/16
 *
 * @author Mr.Feng
 * 简述: 首页商品中的适配器
 */
public class HomeGoodsRecyclerViewAdapter extends RecyclerView.Adapter<HomeGoodsRecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private List<GoodsInfo> dataList;

    public HomeGoodsRecyclerViewAdapter(Context context, List<GoodsInfo> dataList) {
        mContext = context;
        this.dataList = dataList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView goodsTitle;
        private TextView goodsPrice;
        private ImageView goodsImage;

        private OnItemClickListener mListener;// 声明自定义监听接口

        ViewHolder(@NonNull View itemView,OnItemClickListener listener) {
            super(itemView);
            mListener = listener;
            itemView.setOnClickListener(this); // 为ItemView添加点击事件

            goodsTitle = itemView.findViewById(R.id.goods_title);
            goodsPrice = itemView.findViewById(R.id.goods_price);
            goodsImage = itemView.findViewById(R.id.goods_image);
        }

        @Override
        public void onClick(View v) {
            mListener.onItemClick(v, getLayoutPosition());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_goods_rv_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view,mClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GoodsInfo goodsInfo = dataList.get(position);
        holder.goodsTitle.setText(goodsInfo.getGoodsDesc());
        holder.goodsPrice.setText(goodsInfo.getGoodsDefaultPrice());
//        holder.goodsImage.i(goodsInfo.getGoodsDefaultPrice());
        ImageLoader.load(holder.goodsImage.getContext(), goodsInfo.getGoodsDefaultIcon(), holder.goodsImage);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private OnItemClickListener mClickListener;//自定义的接口

    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }

    public void setItemClickListener(OnItemClickListener clickListener) {
        mClickListener = clickListener;
    }
}
